﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.Mtbl;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.DALFactory;
using System.Linq.Dynamic;
namespace HKEA.CMS.Core.DAL.Mtbl
{
    public class mtblCountryDAL:ImtblCountry
    {
        HKEA_CMSEntities model;
        mtblCountry country;
        public mtblCountryDAL()
        {
            model = DataAccess.GetModelInstance();
        }
        public static mtblCountryDAL GetInstance()
        {
            return new mtblCountryDAL();
        }
        public int InsertENT(mtblCountry ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.mtblCountries.AddObject(ent);
            return model.SaveChanges();
        }

        public List<mtblCountry> GetListByENT(mtblCountry ent, PageParam param, string strPropertyName,string strOrderBy)
        {
            var result = model.mtblCountries.Where(a => a.CountryUID > 0).Where(a=>a.Deleted==ent.Deleted);
            if (ent.CountryUID > 0)
                result = result.Where(a => a.CountryUID == ent.CountryUID);
            if (ent.AreaUID > 0)
                result = result.Where(a=>a.AreaUID==ent.AreaUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);

            param.Count = result.Count();
            result = result.OrderBy(strPropertyName + " " + strOrderBy).Skip((param.CurrentPage - 1) * param.PageSize).Take(param.PageSize);
            return result.ToList();
        }
        public List<mtblCountry> GetListByENT(mtblCountry ent)
        {
            var result = model.mtblCountries.Where(a => a.CountryUID > 0).Where(a=>a.Deleted==ent.Deleted);
            if (ent.CountryUID > 0)
                result = result.Where(a => a.CountryUID == ent.CountryUID);
            if (ent.AreaUID > 0)
                result = result.Where(a=>a.AreaUID==ent.AreaUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.ToList();
        }

        public int GetCountByENT(mtblCountry ent)
        {
            var result = model.mtblCountries.Where(a => a.CountryUID > 0).Where(a => a.Deleted == ent.Deleted); ;
            if (ent.CountryUID > 0)
                result = result.Where(a => a.CountryUID == ent.CountryUID);
            if (ent.AreaUID > 0)
                result = result.Where(a=>a.AreaUID==ent.AreaUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.Count();
        }

        public int UpdateENT(mtblCountry ent)
        {
            country = model.mtblCountries.Where(a => a.CountryUID ==ent.CountryUID).FirstOrDefault();
            if (country != null)
            {
                country.AreaUID = ent.AreaUID;
                country.Country_Chi = ent.Country_Chi;
                country.Country_Code = ent.Country_Code;
                country.Country_Eng = ent.Country_Eng;
                country.Deleted = ent.Deleted;
                country.LastModifyBy = ent.LastModifyBy;
                country.LastModifyDate = DateTime.Now;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(mtblCountry ent)
        {
            country = model.mtblCountries.Where(a => a.CountryUID == ent.CountryUID).FirstOrDefault();
            if (country != null)
            {
                model.mtblCountries.DeleteObject(country);
            }
            return model.SaveChanges();
        }
    }
}
